<?php
session_start();
if (!isset($_SESSION['admin']))
    header('location:index.php');
ob_start();
?>

<!DOCTYPE HTML>
<html>

    <head>
        <title>3D|didongde 3D- Di động đây</title>
        <?php include('modules/page_head.php'); ?>
    </head>

    <body>
        <div id="main">
            <div id="header">
                <div id="logo">
                    <h1><a href="index.php">Didongde</a></h1>
                    <div class="slogan">Where you can find your own best suitable mobile</div>
                    <?php include('modules/logo_header.php'); ?>
                </div>
                <div id="menubar">
                    <?php include('modules/menubar.php'); ?>
                </div>
            </div>
        </div>
        <div id="site_content">
            <div id="sidebar_container">
                <h2>Tìm kiếm nhanh</h2>
                <div class="sidebar">
                    <?php
                    include ("modules/quick_search.php");
                    show_manu();
                    ?>
                </div>
                <div class="sidebar">
                    <?php show_price(); ?>
                </div>
            </div>
            <div id="content">
                <!-- insert the page content here -->
                <h2>Xóa tài khoản người dùng</h2>
                <form name="register" method="post" action="#">
                    <table>
                        <tr><th>Tài khoản cần xóa</th></tr>
                        <tr><td style="vertical-align: top;">Tên tài khoản</td><td>
                                <input type="text" name="username" placeholder="Nhập username" <?php if (isset($_POST['username']))
                        echo "value=\"{$_POST['username']}\""; ?>/>
                            </td></tr>
                        <tr><td></td><td>
                                <input type="submit" name="submit" value="Gửi yêu cầu" style="padding:3px 5px 3px 5px;"/>
                            </td></tr>
                    </table>
                </form>

                <div>
                    <?php
                    /* connection to database server */
                    include_once('conf/dbconfig.php');
                    $cfg = new dbconfig();
                    $host = $cfg->get_dbhost();
                    $db = $cfg->get_dbname();
                    $usr = $cfg->get_dbusername();
                    $pass = $cfg->get_dbpwd();
                    $connection = pg_connect("host = $host dbname = $db user = $usr password = $pass")
                            or die("Cannot connect to database server: " . pg_last_error());

                    if (isset($_POST['submit'])) {
                        $username = $_POST['username'];
                        $query = "select * from customer where username = '" . $username . "'";
                        $result = pg_query($connection, $query);
                        if (pg_num_rows($result) <= 0)
                            echo("Tài khoản này không tồn tại");
                        else {
                            setcookie("username", $username, time() + 3600);
                            ob_end_flush();
                            echo "<form method=\"post\" action=\"#\">";
                            echo "<table>";
                            echo "<tr><td>Tên tài khoản</td><td>";
                            echo pg_fetch_result($result, 0, 'username') . "</td></tr>";
                            echo "<tr><td>Địa chỉ</td><td>";
                            echo pg_fetch_result($result, 0, 'address') . "</td></tr>";
                            echo "<tr><td>Số điện thoại</td><td>";
                            echo pg_fetch_result($result, 0, 'phonenumber') . "</td></tr>";
                            echo "<tr><td>Nghề nghiệp</td><td>";
                            echo pg_fetch_result($result, 0, 'occupation') . "</td></tr>";
                            echo "<tr><td>Tài khoản số</td><td>";
                            echo pg_fetch_result($result, 0, 'accno') . "</td></tr>";
                            echo "<tr><td>Ngân hàng</td><td>";
                            echo pg_fetch_result($result, 0, "banks") . "</td></tr>";
                            echo "<tr><td></td><td>";
                            echo "<input type=\"submit\" name=\"del\" value=\"Xóa tài khoản này\" style=\"padding:3px 5px 3px 5px;\"  onclick=\"return confirm('Bạn chắc chắn muốn xóa người sử dụng này?')\"/>";
                            echo "</td></tr></table>";
                        }
                    }
                    if (isset($_POST['del'])) {
                        $username = $_COOKIE['username'];
                        $query = "select * from customer";
                        $result = pg_query($connection, $query);
                        $row = pg_num_rows($result);
                        $query = "select * from customer where username = '" . $username . "'";
                        $result = pg_query($connection, $query);
                        $id = pg_fetch_result($result, 0, 'customerid');
                        $id = strtok($id, "C");
                        $query = "delete from customer where username ='" . $username . "'";
                        $result = pg_query($connection, $query);
                        if (pg_affected_rows($result)) {
                            for ($i = $id + 1; $i <= $row; $i++) {
                                $cusid = strval($i);
                                while (strlen($cusid) < 3)
                                    $cusid = "0" . $cusid;
                                $new_id = strval($i - 1);
                                while (strlen($new_id) < 3)
                                    $new_id = "0" . $new_id;
                                $cusid = "C" . $cusid;
                                $new_id = "C" . $new_id;
                                $query = "update customer set customerid = '$new_id' where customerid = '$cusid'";
                                $result = pg_query($connection, $query);
                            }
                            echo "Xóa thành công tài khoản " . $username;
                        }
                    }
                    ?>
                </div>

            </div>
        </div>
    </div>
    <div id="footer">
        <?php include('modules/footer.php'); ?>
    </div>
</body>
</html>

